package cn.sxw.ld.module.member.service.impl;


import cn.sxw.ld.common.utils.DateUtils;
import cn.sxw.ld.module.member.mapper.CostRecordMapper;
import cn.sxw.ld.module.member.pojo.CostPageDto;
import cn.sxw.ld.module.member.pojo.CostRecord;
import cn.sxw.ld.module.member.pojo.Member;
import cn.sxw.ld.module.member.service.CostRecordService;
import cn.sxw.ld.module.member.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author: SXW
 * @since: 2025年02月20日 13:10
 * @desc:
 */
@Service
public class CostRecordServiceImpl extends ServiceImpl<CostRecordMapper, CostRecord> implements CostRecordService {

    @Autowired
    private MemberService memberService;

    /**
     * 依据回传信息，查询分页信息
     *
     * @return
     */
    @Override
    public Page getPage(CostPageDto dto) {
        Page<CostRecord> pageInfo = new Page<>(dto.getPage(), dto.getPageSize());
        String phone = dto.getPhone();
        // 如果没有回传手机号则直接返回报错
        if (StringUtils.isBlank(phone)) {
            return null;
        }
        LambdaQueryWrapper<CostRecord> costWrapper = new LambdaQueryWrapper<>();
        costWrapper.eq(CostRecord::getPhone, phone);
        costWrapper.eq(dto.getBsType() != null, CostRecord::getBsType, dto.getBsType());
        costWrapper.between(StringUtils.isNoneBlank(dto.getBeginTime()) && StringUtils.isNoneBlank(dto.getEndTime()),
                CostRecord::getCreateTime, dto.getBeginTime(), dto.getEndTime());
        costWrapper.orderByDesc(CostRecord::getCreateTime);
        this.page(pageInfo, costWrapper);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CostRecord addRecord(CostRecord CostRecord) {
        // 本次业务金额
        BigDecimal costAmount = CostRecord.getCostAmount();
        // 获取用户余额
        Member member = memberService.getById(CostRecord.getPhone());
        BigDecimal balance = member.getBalance();
        // 累计充值
        BigDecimal totalRecharge = member.getTotalRecharge();
        switch (CostRecord.getBsType()) {
            case 1: // 充值
                balance = balance.add(costAmount);
                totalRecharge = totalRecharge.add(costAmount);
                break;
            case 2: // 消费
                BigDecimal decimal = balance.subtract(costAmount);
                // 消费金额小于 0 直接返回余额不足
                if (decimal.compareTo(BigDecimal.ZERO) < 0) {
                    return null;
                } else {
                    // 将计算后余额赋值查询值
                    balance = decimal;
                }
                break;
        }
        // 保存新余额
        member.setBalance(balance);
        // 累计充值
        member.setTotalRecharge(totalRecharge);
        memberService.updateById(member);
        // 生成记录
        CostRecord.setBalance(balance);
        this.save(CostRecord);
        return CostRecord;
    }

}
